package com.example.yunhailutong.zhongtan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.ActivityNewGongYing;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class FragmentNewGongYing extends BaseFragment {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;

    Unbinder unbinder;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.new_gongying)
    LinearLayout newGongying;
    @BindView(R.id.new_xuqiu)
    LinearLayout newXuqiu;

    private View view;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.fragment_new_gongying, container, false);
        // TODO: add setContentView(...) invocation


        unbinder = ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }

        Map<String, String> params = new HashMap<>();
        params.put("mmId", App.id);
        params.put("token", App.token);
        if(App.isLogin){
            new PersionBiz().isHaveMessage(params);
        }else{

        }
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Map<String, String> params = new HashMap<>();
                params.put("mmId", App.id);
                params.put("token", App.token);
                if(App.isLogin){
                    new PersionBiz().isHaveMessage(params);
                }else{

                }
                refreshlayout.finishRefresh();
            }
        });
        return view;
    }

    @Subscribe
    public void log(OldsEvent.IsHaveMessageEvent msg) {
        if (msg.what == -1) {
            newGongying.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disPlay("尚未完善个人资料，请完善个人资料后重试");
                }
            });
            newXuqiu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disPlay("尚未完善个人资料，请完善个人资料后重试");
                }
            });
        }
        if (msg.what == 1) {
            newGongying.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ActivityNewGongYing.class);
                    intent.putExtra("type", "1");

                    startActivity(intent);
                }
            });
            newXuqiu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ActivityNewGongYing.class);
                    intent.putExtra("type", "2");
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
